/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.worksheet.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.SerialNumberUtils;
import com.thinkgem.jeesite.modules.worksheet.entity.ZlmtWorksheetFiles;
import com.thinkgem.jeesite.modules.worksheet.service.ZlmtResourceWorksheetService;
import com.thinkgem.jeesite.modules.worksheet.service.ZlmtWorksheetFilesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.worksheet.entity.ZlmtWorksheet;
import com.thinkgem.jeesite.modules.worksheet.service.ZlmtWorksheetService;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单管理Controller
 *
 * @author johnKee
 * @version 2016-07-17
 */
@Controller
@RequestMapping(value = "${adminPath}/worksheet/zlmtWorksheet")
public class ZlmtWorksheetController extends BaseController {

    @Autowired
    private ZlmtWorksheetService zlmtWorksheetService;
    @Autowired
    private ZlmtWorksheetFilesService zlmtWorksheetFilesService;
    @Autowired
    private ZlmtResourceWorksheetService zlmtResourceWorksheetService;

    @ModelAttribute
    public ZlmtWorksheet get(@RequestParam(required = false) String id) {
        ZlmtWorksheet entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = zlmtWorksheetService.get(id);
            List<ZlmtWorksheetFiles> list = zlmtWorksheetFilesService.findByParams(id);
            if (list != null && list.size() > 0) {
                if (list.size() == 1) {
                    entity.setZlmtWorksheetFile(list.get(0));
                } else {
                    entity.setZlmtWorksheetFiles(list);
                }
            }
            entity.setZlmtResourceWorksheet(zlmtResourceWorksheetService.findByParams(id));
        }
        if (entity == null) {
            entity = new ZlmtWorksheet();
        }
        return entity;
    }

    @RequestMapping(value = {"list", ""})
    public String list(ZlmtWorksheet zlmtWorksheet, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ZlmtWorksheet> page = zlmtWorksheetService.findPage(new Page<ZlmtWorksheet>(request, response), zlmtWorksheet);
        model.addAttribute("page", page);
        return "modules/worksheet/zlmtWorksheetList";
    }

    @RequestMapping(value = "handingExceptionForm")
    public String handingExceptionForm(ZlmtWorksheet zlmtWorksheet, Model model, String flag) {
        return returnPage(zlmtWorksheet, model, flag);
    }

    @RequestMapping(value = "bizApplyForm")
    public String bizApplyForm(ZlmtWorksheet zlmtWorksheet, Model model, String flag) {
        return returnPage(zlmtWorksheet, model, flag);
    }

    private String returnPage(ZlmtWorksheet zlmtWorksheet, Model model, String flag) {
        String view = flag + "WorksheetForm"; //启动流程
        if (StringUtils.isNotBlank(zlmtWorksheet.getId())) {
            List<ZlmtWorksheetFiles> list = zlmtWorksheet.getZlmtWorksheetFiles(); //获取相关资源列表、相关附件进行显示
            if (list != null && list.size() > 0) {
                ZlmtWorksheetFiles files = new ZlmtWorksheetFiles();
                StringBuilder urls = new StringBuilder();
                for (ZlmtWorksheetFiles f : list) {
                    urls.append("|" + f.getFileUrl());
                    files.setWorksheetId(f.getWorksheetId());
                }
                files.setFileUrl(urls.toString());
                zlmtWorksheet.setZlmtWorksheetFile(files);
            }

            String taskDefKey = zlmtWorksheet.getAct().getTaskDefKey();
            if (zlmtWorksheet.getAct().isFinishTask()) {
                view = flag + "WorksheetView";
            } else if (taskDefKey.contains("do") || taskDefKey.contains("Do")) {
                view = flag + "WorksheetDoForm";
            } else if (taskDefKey.contains("outsideDeal")) {
                view = flag + "WorksheetOutsideDealForm";
            } else {
                view = flag + "WorksheetAuditView";
            }
        } else {
            String worksheetCode = SerialNumberUtils.getWSNum("BZ");
            zlmtWorksheet.setWorksheetCode(worksheetCode);
        }
        model.addAttribute("zlmtWorksheet", zlmtWorksheet);
        return "modules/worksheet/" + view;
    }

    @RequestMapping(value = "form")
    public String form(ZlmtWorksheet zlmtWorksheet, Model model) {
        model.addAttribute("zlmtWorksheet", zlmtWorksheet);
        return "modules/worksheet/zlmtWorksheetForm";
    }

    @RequestMapping(value = "save")
    public String save(ZlmtWorksheet zlmtWorksheet, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, zlmtWorksheet)) {
            return form(zlmtWorksheet, model);
        }
        zlmtWorksheetService.save(zlmtWorksheet);
        addMessage(redirectAttributes, "提交审批成功");
        return "redirect:" + adminPath + "/act/task/todo/";
    }

    @RequestMapping(value = "auditSave")
    public String saveAudit(ZlmtWorksheet zlmtWorksheet, Model model) {
        if (StringUtils.isBlank(zlmtWorksheet.getAct().getFlag())
                || StringUtils.isBlank(zlmtWorksheet.getAct().getComment())) {
            addMessage(model, "请填写审核意见。");
            return form(zlmtWorksheet, model);
        }
        zlmtWorksheetService.auditSave(zlmtWorksheet);
        return "redirect:" + adminPath + "/act/task/todo/";
    }

    @RequiresPermissions("worksheet:zlmtWorksheet:edit")
    @RequestMapping(value = "delete")
    public String delete(ZlmtWorksheet zlmtWorksheet, RedirectAttributes redirectAttributes) {
        zlmtWorksheetService.delete(zlmtWorksheet);
        addMessage(redirectAttributes, "删除工单管理成功");
        return "redirect:" + Global.getAdminPath() + "/worksheet/zlmtWorksheet/?repage";
    }

}